package com.jason.demo.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jason.demo.pojo.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/user")
@Api(value = "User", tags = {"用户相关"})
public class UserController {
	
	// 创建线程安全的Map
		static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>() {{
			put(1L, new User(1L, "jason", 22, "java", 5500D));
			put(2L, new User(2L, "lily", 12, "java", 500D));
			put(3L, new User(3L, "boy", 22, "java", 7500D));
			put(4L, new User(4L, "james", 28, "java", 15500D));
		}});
		
		@GetMapping("/")
	    @ApiOperation(value = "查询所有用户")
	    @ApiImplicitParams({
	            @ApiImplicitParam(value = "ID", name = "id", dataType = "int", paramType = "path", required = true, defaultValue = "1")
	    })
	    @ApiResponses({
	            @ApiResponse(code = 400, message = "请求参数有误"),
	            @ApiResponse(code = 401, message = "未授权"),
	            @ApiResponse(code = 403, message = "禁止访问"),
	            @ApiResponse(code = 404, message = "请求路径不存在"),
	            @ApiResponse(code = 500, message = "服务器内部错误")
	    })
		@RequestMapping(value = "/", method = RequestMethod.GET)
		public List<User> getUserList() {
			return new ArrayList<>(users.values());
		}
		
		@PostMapping("")
	    @ApiOperation(value = "创建用户")
	    @ApiResponses({
	            @ApiResponse(code = 400, message = "请求参数有误"),
	            @ApiResponse(code = 401, message = "未授权"),
	            @ApiResponse(code = 403, message = "禁止访问"),
	            @ApiResponse(code = 404, message = "请求路径不存在"),
	            @ApiResponse(code = 500, message = "服务器内部错误")
	    })
		@RequestMapping(value = "/", method = RequestMethod.POST)
		public String postUser(@ModelAttribute User user) {
			if (user == null) {
				return "error";
			}
			// @ModelAttribute:除了绑定参数之外，还可以通过@RequestParam从页面中传递参数
			users.put(user.getUid(), user);
			return "success";
		}
		
		@GetMapping("/{id}")
	    @ApiOperation(value = "使用ID查询用户")
	    @ApiImplicitParams({
	            @ApiImplicitParam(value = "ID", name = "id", dataType = "int", paramType = "path", required = true, defaultValue = "1")
	    })
	    @ApiResponses({
	            @ApiResponse(code = 400, message = "请求参数有误"),
	            @ApiResponse(code = 401, message = "未授权"),
	            @ApiResponse(code = 403, message = "禁止访问"),
	            @ApiResponse(code = 404, message = "请求路径不存在"),
	            @ApiResponse(code = 500, message = "服务器内部错误")
	    })
		@RequestMapping(value = "/{id}", method = RequestMethod.GET)
		public User getUser(@PathVariable Long id) {
			// url中的id可以通过@PathVariable注解绑定到函数的参数中
			return users.get(id);
		}
		
		/**
		 * 根据id来更新User
		 */
		@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
		public String putUser(@PathVariable Long id, @ModelAttribute User user) {
			User u = users.get(id);
			if (u == null) {
				return "this user not found. update error";
			}
			users.replace(id, user);
			return "success";
		}
		
		/**
		 * 根据id来删除User
		 * @param id
		 * @return
		 */
		@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
		public String deleteUser(@PathVariable Long id) {
			User u = users.get(id);
			if (u == null) {
				return "this user not found. delete error";
			}
			users.remove(id);
			return "success";
		}
	
}